package com.moke.loader
{	
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.ProgressEvent;
 
	/**
	 * 
	 * @author fraser
	 *
	 */
	public class UILoader
	{
		private static var _loader:IntegrateLoader = new IntegrateLoader();

		/**
		 * 开始加载
		 * 
		 * @param url       地址
		 * @param type      类型 LoadType
		 * @param complete  加载完成方法回调
		 * @param error     加载错误方法回调
		 * @param title  	LoadingBar显示的标题
		 * @param open      加载开始方法回调
		 * @param progress  加载进度方法回调
		 *
		 */
		public static function load(url:String,type:String,complete:Function,error:Function=null,title:String="",data:*=null,open:Function=null,progress:Function=null):void
		{
			if (_loader.info)
			{
				if (_loader.info.url == url)
				{
					return;
				}
			}
			var info:QueueInfo = new QueueInfo();
			info.url = url;
			info.type = type;
			info.title = title;
			info.data = data;
			info.completeHandler = complete;
			info.errorHandler = error;
			info.openHandler = open;
			info.progressHandler = progress;
			//
			doLoad(info);
		}

		/**
		 * 取消当前加载
		 * @param complete 加载完成的 回调方法
		 *
		 */
		public static function cancel(complete:Function):void
		{
			if (_loader.info)
			{
				if (_loader.info.completeHandler == complete)
				{
					close();
				}
			}
		}

		//--------------------------------------------------
		// private
		//--------------------------------------------------

		private static function doLoad(info:QueueInfo):void
		{
			_loader.addEventListener(Event.OPEN,onOpen);
			_loader.addEventListener(Event.COMPLETE,onComplete);
			_loader.addEventListener(ProgressEvent.PROGRESS,onProgress);
			_loader.addEventListener(ErrorEvent.ERROR,onError);
			_loader.load(info);
		}

		private static function close():void
		{
			_loader.removeEventListener(Event.OPEN,onOpen);
			_loader.removeEventListener(Event.COMPLETE,onComplete);
			_loader.removeEventListener(ProgressEvent.PROGRESS,onProgress);
			_loader.removeEventListener(ErrorEvent.ERROR,onError);
			_loader.close();
		}

		private static function onOpen(event:Event):void
		{
			var info:QueueInfo = _loader.info;
			if (info.openHandler != null)
			{
				info.openHandler(new ContentInfo(info.url,info.type,null,null,info.data));
			}
		}

		private static function onComplete(event:IntegrateLoaderEvent):void
		{
			var info:QueueInfo = _loader.info;
			close();
			if (info.completeHandler != null)
			{
				info.completeHandler(new ContentInfo(info.url,info.type,event.content,event.domain,info.data));
			}
			info.dispose();
		}

		private static function onError(event:ErrorEvent):void
		{
			var info:QueueInfo = _loader.info;
			close();
			if (info.errorHandler != null)
			{
				info.errorHandler(new ContentInfo(info.url,info.type,null,null,info.data));
			}
			info.dispose();
		}

		private static function onProgress(event:ProgressEvent):void
		{
			var info:QueueInfo = _loader.info;
			if (info.progressHandler != null)
			{
				info.progressHandler(event);
			}
		}
	}
}
